home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC Graphics Unleashed
/
PC Graphics Unleashed.iso
/
ch19
/
prog3.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-09
|
5KB
|
235 lines
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
#include <dos.h>
#define PI 3.1415926
#define EPSILON 1E-3
void xrotate(float *, int);
void yrotate(float *, int);
void zrotate(float *, int);
void getmouse_xy(int *, int *, int *);
float st[361], ct[361];
union REGS regs;
void main(void)
{
int i,j,k,l;
int height, height2;
unsigned char x,y,z;
int xx, yy, button;
int tx, ty, tz;
float point[3];
int view[100][100];
unsigned char bubblemass[70][70][70];
unsigned char bubble[21][21][21];
int grdriver, grmode, grerror;
/*define sine and cosine tables*/
for(i=0; i<=360; i++)
{
st[i]=(float)sin((double)(2*PI/360.0*i));
ct[i]=(float)cos((double)(2*PI/360.0*i));
}
/*MAKE BUBBLE*/
for(i=-10; i < 11; i++)
{
height = (int)ceil(10.0*sin(acos((double)i/10.0)));
if(height != 0)
for(j=-height; j < height+1; j++)
{
height2 = (int)ceil(height*sin(acos((double)j/height)));
if(height2 !=0)
for(k=-height2; k < height2+1; k++)
bubble[i+10][j+10][k+10] = 1;
}
}
/*INITIATE BUBBLE MASS*/
for(i=0; i < 70; i++)
for(j=0; j < 70; j++)
for(k=0; k < 70; k++)
bubblemass[i][j][k] = 0;
/*CREATE BUBBLE MASS*/
for(l=0; l < 100; l++)
{
x = (unsigned char)(((float)(rand())*35/(float)RAND_MAX)+10);
y = (unsigned char)(((float)(rand())*35/(float)RAND_MAX)+10);
z = (unsigned char)(((float)(rand())*35/(float)RAND_MAX)+10);
for(i=0; i < 21; i++)
for(j=0; j < 21; j++)
for(k=0; k < 21; k++)
if(bubble[i][j][k] == 1)
if(bubblemass[x+i][y+j][z+k] < 15)
bubblemass[x+i][y+j][z+k] ++;
}
/*INITIALIZE GRAPHICS SCREEN*/
grdriver = VGA256;
grmode = 0;
initgraph(&grdriver, &grmode, "");
grerror = graphresult();
if(grerror)
{
closegraph();
printf("Error Initializing Graphics Mode.\n");
exit(1);
}
/*MOUSE INITIALIZATION*/
regs.w.ax = 0;
int386(0x33, ®s, ®s);
if((short)regs.w.ax != -1)
{
closegraph();
printf("Mouse driver must be installed before running ");
printf("this program.\n");
exit(1);
}
regs.w.ax = 0x0007;
regs.w.cx = 0;
regs.w.dx = 0x0280;
int386(0x33, ®s, ®s);
regs.w.ax = 0x0008;
regs.w.cx = 0;
regs.w.dx = 0x00c8;
int386(0x33, ®s, ®s);
regs.w.ax = 0x0001;
int386(0x33, ®s, ®s);
/*MIP IMAGE DISPLAY*/
while(!kbhit())
{
/*GET ROTATION VALUES FROM MOUSE*/
getmouse_xy(&xx, &yy, &button);
tx = (int)((float)(yy-100)*1.8);
ty = (int)((float)(xx-320)*0.5625);
while((tx < 0)||(tx > 360)||(ty < 0)||(ty > 360))
{
if(tx < 0)
tx = tx + 360;
if(tx > 360)
tx = tx - 360;
if(ty < 0)
ty = ty + 360;
if(ty > 360)
ty = ty - 360;
}
/*CLEAR PROJECTION SURFACE*/
for(i=0; i<100; i++)
for(j=0; j<100; j++)
view[i][j] = 0;
/*ROTATE AND CREATE MIP IMAGE*/
for(i=0; i < 70; i++)
for(j=0; j < 70; j++)
for(k=0; k < 70; k++)
if(bubblemass[i][j][k] > 0)
{
point[0] = (float)(i - 35);
point[1] = (float)(j - 35);
point[2] = (float)(k - 35);
xrotate(point,tx);
yrotate(point,ty);
if(view[(int)(point[0]+50.0)][(int)(point[1]+50.0)]
< bubblemass[i][j][k])
view[(int)(point[0]+50.0)][(int)(point[1]+50.0)]
= bubblemass[i][j][k];
}
/*PLOT MIP IMAGE ONTO GRAPHICS SCREEN*/
for(i=0; i<100; i++)
for(j=0; j<100; j++)
{
putpixel(i+110, j+50, 16+view[i][j]);
}
}
getch();
closegraph();
}
void xrotate(float *point, int deg)
{
float y,z;
y = point[1]*ct[deg] + point[2]*st[deg];
z = point[2]*ct[deg] - point[1]*st[deg];
point[1] = y;
point[2] = z;
}
void yrotate(float *point, int deg)
{
float x,z;
x = point[0]*ct[deg] - point[2]*st[deg];
z = point[0]*st[deg] + point[2]*ct[deg];
point[0] = x;
point[2] = z;
}
void zrotate(float *point, int deg)
{
float x,y;
x = point[0]*ct[deg] + point[1]*st[deg];
y = point[1]*ct[deg] - point[0]*st[deg];
point[0] = x;
point[1] = y;
}
void getmouse_xy(int *xx, int *yy, int *button)
{
regs.w.ax=0x0003;
int386(0x33, ®s, ®s);
*xx=(int)regs.w.cx;
*yy=(int)regs.w.dx;
*button=regs.w.bx;
}